package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.LinaerGradientTextView;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static com.kanshu.ksgb.fastread.doudou.app.constants.Constants.BookCityModuleType.TYPE_SELECTED_RENQIZHUANQU;
import static com.kanshu.ksgb.fastread.doudou.base.baseui.SwipeBackActivity.ACTION_DESTORY;

/**
 * Created by wcy on 18/5/15.
 */

public class AdSelectedEightLayout extends LinearLayout implements IRefresh<SelectedBean> {


    @BindView(R.id.label)
    TextView mLabel;
    @BindView(R.id.grid_container)
    GridLayout mGridContainer;
    ItemRefreshBean mItemRefreshBean;
    @BindView(R.id.change)
    TextView mChange;
    boolean mIsFirst;
    @BindView(R.id.change_tip)
    ImageView mChangeTip;
    HashMap<String, Boolean> mShowNormalDatas = new HashMap<>();
    String mType;

    public Subject<Integer> lifeCyclerSubject = PublishSubject.create();
    BookCityPresenter presenter;
    public AdSelectedEightLayout(Context context) {
        super(context);
        init();
    }

    public AdSelectedEightLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedEightLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        presenter = new BookCityPresenter(lifeCyclerSubject);
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_selected_eight_layout, this);
        ButterKnife.bind(view);

    }

    @Override
    public void refresh(SelectedBean selectedBean, Object object) {
        if (selectedBean == null || object == null) {
            return;
        }
        BookCityUtils.rotateCancel(mChangeTip);
        mItemRefreshBean = (ItemRefreshBean) object;
        mLabel.setText(selectedBean.name);
        mLabel.setCompoundDrawablesWithIntrinsicBounds(mItemRefreshBean.selectionIcon, 0, 0, 0);
        if (Constants.BookCityModuleType.TYPE_MALE_HUANFENLEI2.equals(mItemRefreshBean.type)
                || Constants.BookCityModuleType.TYPE_FEMALE_HUANFENLEI2.equals(mItemRefreshBean.type)) {
            mChange.setText("换分类");
        } else {
            mChange.setText("换一换");
        }
        if (!Utils.isEmptyList(selectedBean.list)) {
            if (mShowNormalDatas.get(mType) != null
                    && mShowNormalDatas.get(mType)) {
                return;
            }
            for (int i = 0; i < 4; i++) {
                GridLayout.LayoutParams params = (GridLayout.LayoutParams) mGridContainer.getChildAt(i).getLayoutParams();
                if (!mIsFirst) {
                    params.width = (DisplayUtils.getScreenWidth(mGridContainer.getContext()) - DisplayUtils.dip2px(mGridContainer.getContext(), 64)) / 2;
                    mGridContainer.getChildAt(i).setLayoutParams(params);
                }
                mGridContainer.getChildAt(i).setVisibility(View.GONE);
            }
            mIsFirst = true;
            if (selectedBean.list.size() > 4) {
                selectedBean.list = selectedBean.list.subList(0, 4);
            }
            for (int i = 0; i < selectedBean.list.size(); i++) {
                LinearLayout linearLayout = (LinearLayout) mGridContainer.getChildAt(i);
                linearLayout.setVisibility(VISIBLE);
                LinearLayout linearLayout1 = (LinearLayout) linearLayout.getChildAt(0);
                ImageView imageView = (ImageView) linearLayout.getChildAt(1);
                TextView tv1 = (TextView) linearLayout1.getChildAt(0);
                LinaerGradientTextView tv2 = (LinaerGradientTextView) linearLayout1.getChildAt(1);
                if (TextUtils.equals(selectedBean.type, Constants.BookCityModuleType.TYPE_SELECTED_RENQIZHUANQU)) {
                    tv2.setLinearGradientColor(Color.parseColor("#ff4a62"), Color.parseColor("#ff8745"));
                } else if (TextUtils.equals(selectedBean.type, Constants.BookCityModuleType.TYPE_MALE_XUANHUANKEHUAN)) {
                    tv2.setLinearGradientColor(Color.parseColor("#61a6f2"), Color.parseColor("#38c4dd"));
                } else if (TextUtils.equals(selectedBean.type, Constants.BookCityModuleType.TYPE_FEMALE_ZONGCAIHAOMEN)) {
                    tv2.setLinearGradientColor(Color.parseColor("#ff4a62"), Color.parseColor("#ff8475"));
                } else if (TextUtils.equals(selectedBean.type, Constants.BookCityModuleType.TYPE_MALE_HUANFENLEI2)) {
                    tv2.setLinearGradientColor(Color.parseColor("#61a6f2"), Color.parseColor("#38c4dd"));
                } else if (TextUtils.equals(selectedBean.type, Constants.BookCityModuleType.TYPE_FEMALE_HUANFENLEI2)) {
                    tv2.setLinearGradientColor(Color.parseColor("#ff4a62"), Color.parseColor("#ff8475"));
                }
                GlideImageLoader.load(selectedBean.list.get(i).cover_url, imageView);
                tv1.setText(selectedBean.list.get(i).book_title);
                tv2.setText(BookUtils.formatNum(selectedBean.list.get(i).total_click_num) + "人在读");
                final  int pos = i;
                linearLayout.setOnClickListener(v -> {
                    ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", selectedBean.list.get(pos).book_id);
                    BookCityUtils.takeIf(mItemRefreshBean, TYPE_SELECTED_RENQIZHUANQU, () -> {
                        AdPresenter.pvuvStatics(pos,
                                R.string.SCTUIJIAN8_POSITION1,
                                R.string.SCTUIJIAN8_POSITION2,
                                R.string.SCTUIJIAN8_POSITION3,
                                R.string.SCTUIJIAN8_POSITION4);
                    });
                });
            }
            mShowNormalDatas.put(mType, true);
        }
    }

    @Override
    public void onError(int code, String desc) {
        BookCityUtils.rotateCancel(mChangeTip);
    }

    @Override
    public void reset() {
        if (mShowNormalDatas != null) {
            mShowNormalDatas.clear();
        }
    }

    @OnClick(R.id.change_container)
    public void onViewClicked() {
        mShowNormalDatas.put(mType, false);
        BookCityUtils.rotate(mChangeTip);
        if (Constants.BookCityModuleType.TYPE_MALE_HUANFENLEI2.equals(mItemRefreshBean.type)) {
            BookCityUtils.switchMaleCategory(mItemRefreshBean, this, null,presenter);
        } else if (Constants.BookCityModuleType.TYPE_FEMALE_HUANFENLEI2.equals(mItemRefreshBean.type)) {
            BookCityUtils.switchFemaleCategory(mItemRefreshBean, this, null,presenter);
        } else {
            BookCityUtils.refreshModuleInfos(mItemRefreshBean, this,presenter);
        }

        BookCityUtils.takeIf(mItemRefreshBean, TYPE_SELECTED_RENQIZHUANQU, () -> {
            AdPresenter.pvuvStatics(R.string.SCTUIJIAN8_HUANYIHUAN);
        });

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        lifeCyclerSubject.onNext(ACTION_DESTORY);
    }
}
